<?php
/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/

?>
<?php
class Admin_Form_Channel extends Zend_Form{
	private $_channel;

	public function __construct( Default_Model_Channel $channel, $options = null ){
		parent::__construct( $options );

		$this->_channel = $channel;

		$types = array(
			1	=> Default_Model_Channel::TYPE_1,
			2	=> Default_Model_Channel::TYPE_2,
		//	3	=> Default_Model_Channel::TYPE_3,
		//	4	=> Default_Model_Channel::TYPE_4,
			5	=> Default_Model_Channel::TYPE_5,
		);

		$this->addElement( 'text', 'name', array(
			'value'			=> $channel->name,
			'required'		=> true,
			'label'			=> 'name:',
			'validators'	=>	array(array('StringLength', 254, 1 ))
		));

		$this->addElement( 'textarea', 'description', array(
			'value'			=> $channel->description,
			'required'		=> true,
			'label'			=> 'description:',
			'validators'	=>	array(array('StringLength', 1024, 1 ))
		));

		$this->addElement( 'select', 'type', array(
			'label'			=> 'Channel type',
			'multiOptions'	=> $types
		));

		$this->addElement( 'submit', 'save', array(
			'label'	=> 'Save changes'
		));

        $this->addDisplayGroup( array_keys( $this->getElements() ), 'metadata', array(
            'legend' => 'Channel properties'
        ));

		if( null !== $channel->id ){
			$this->addSubForm( $this->getKeywordsForm(), 'keywords');
		}
	}

	private function getKeywordsForm(){
		$domain   = new Default_Model_Domain();
		$form 	  = new Zend_Form_SubForm();
		$keywords = array();
		$domains  = array();

		$form->setLegend('keywords');

		foreach( $this->_channel->getKeywords() as $keyword ){
			$keywords[$keyword->id] = $keyword->name;
		}
		foreach( $domain->fetchAll() as $domain ){
			$domains[$domain->id] = $domain->name;
		}

		$form->addElement('text', 'keyword_name', array('label'	=> 'New keyword'));
		$form->addElement('select', 'keyword_domain', array(
			'multiOptions'	=> $domains
		));

		$form->addElement('MultiCheckbox', 'selected', array(
			'multiOptions'	=> $keywords,
		));

		foreach( array( 'keyword_name', 'keyword_domain' ) as $name ){
			$element = $form->getElement($name);
			$element->removeDecorator('Label');
			$element->removeDecorator('HtmlTag');
		}

		$element = $form->getElement( 'selected' );
		$element->getDecorator('HtmlTag')->setOption('tag', 'div');
		$element->removeDecorator('Label');

		$form->removeDecorator('HtmlTag');

		$form->addElement( 'submit', 'update', array(
			'label'	=> 'Update labels'
		));

		return $form;
	}

}
